/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.mdc.hearts.war.authorization;

import it.mdc.hearts.war.room.SessionUserBean;
import it.mdc.hearts.war.room.core.UserInfo;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import it.mdc.hearts.ejb.database.Users;
import it.mdc.hearts.ejb.database.UsersFacadeLocal;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "authorizationBean")
@RequestScoped
public class AuthorizationBean implements Serializable {
    @EJB
    private UsersFacadeLocal usersFacade;
    
    @ManagedProperty(value = "#{sessionUserBean}") // MY SESSION BEAN
    private SessionUserBean userSession;
    
    private String errorMessageLogin;
    private String username;
    private String password;


    public void setUserSession(SessionUserBean userSession) {
        this.userSession = userSession;
    }
    
   
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }

    public String getErrorMessageLogin() {
        return errorMessageLogin;
    }
    
    public void setErrorMessageLogin(String errorMessageLogin) {
        this.errorMessageLogin = errorMessageLogin;
    }
    
    public String login()
    {
        Users user;
        user = usersFacade.getUserByUsername(username);            
        if (user == null) 
        {
            FacesContext.getCurrentInstance().addMessage("loginForm", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login Failed!", "The username specified is not correct"));
        }
        else
        {
            if(user.getPasswd().equals(password))
            {
                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("ID", user.getId());
                userSession.setID(user.getId());
                
                UserInfo userInfo = new UserInfo();
                userInfo.setUserID(user.getId());
                userInfo.setUsername(user.getUsername());
                userInfo.setScore(user.getPoint());
                
                userSession.InitUser(userInfo);
               
                return "loginSuccess";   
            }
            else
            {
                FacesContext.getCurrentInstance().addMessage("loginForm", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login Failed!", "The password specified is not correct."));
            }
        }
        
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("ID", null);
        return null;
    }
    
    public String logout() {
        HttpSession sessione = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        if (sessione != null) {
            try {
                userSession.removeUser();
                sessione.invalidate();
                return "loginPage";
            } 
            catch (Exception e) 
            {
                System.err.println("Impossibile Rimuovere La Sessione: " + e);
                return e.getMessage();
            }
        } 
        else 
        {
            System.err.println("Logout: Sessione inesistente");
            return null;
        }
    }
}
